package com.gjzt.business.publish.service;


import com.gjzt.business.publish.inter.PublishBillDetailMapper;
import com.gjzt.business.publish.model.PublishBillDetail;
import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.model.BusinessException;
import com.eastdigit.system.model.SqlBuilder;
import com.eastdigit.system.service.BaseService;
import com.eastdigit.util.DateUtil;
import com.eastdigit.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PublishBillDetailService extends BaseService {

    @Autowired
    private PublishBillDetailMapper publishBillDetailMapper;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONObject findAdPublishBillDetail(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("", new Object[0]);
        return this.queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
     * 新增
     *
     * @param reqBean
     */
    public void addAdPublishBillDetail(ReqBean reqBean) {
        PublishBillDetail publishBillDetail = getAdPublishBillDetail(reqBean);

        publishBillDetailMapper.insert(publishBillDetail);
    }

    /**
     * 修改
     *
     * @param reqBean
     */
    public void updateAdPublishBillDetail(ReqBean reqBean) {
        String billId = reqBean.getInfo("billId");
        String resourceId = reqBean.getInfo("resourceId");
        if (StringUtil.isEmpty(billId) || StringUtil.isEmpty(resourceId)) {
            throw new BusinessException("获取主键失败");
        }
        PublishBillDetail publishBillDetail = getAdPublishBillDetail(reqBean);
        publishBillDetailMapper.updateByPrimaryKeySelective(publishBillDetail);
    }

    /**
     * 删除
     *
     * @param reqBean
     */
    public void removeAdPublishBillDetail(ReqBean reqBean) {
        String id = reqBean.getInfo("id");
        if (StringUtil.isEmpty(id)) {
            throw new BusinessException("获取主键失败");
        }
        publishBillDetailMapper.deleteByPrimaryKey(id);
    }

    /**
     * 从表单获取对象
     *
     * @param reqBean
     * @return
     */
    private PublishBillDetail getAdPublishBillDetail(ReqBean reqBean) {
        PublishBillDetail publishBillDetail = new PublishBillDetail();

        publishBillDetail.setBillId(reqBean.getInfo("billId"));
        publishBillDetail.setResourceId(reqBean.getInfo("resourceId"));
        publishBillDetail.setContent(reqBean.getInfo("content"));
        publishBillDetail.setPublishTime(DateUtil.parseDateTime(reqBean.getInfo("publishTime")));
        publishBillDetail.setPublishOffTime(DateUtil.parseDateTime(reqBean.getInfo("publishOffTime")));
        publishBillDetail.setPublishFile(reqBean.getInfo("publishFile"));
        publishBillDetail.setPublishImg(reqBean.getInfo("publishImg"));
        return publishBillDetail;
    }

    public JSONObject queryDetails(ReqBean reqBean) {
        JSONObject returnJson = new JSONObject();
        JSONArray array = new JSONArray();
        JSONObject dataJson = new JSONObject();
        dataJson.put("billId", "1");
        dataJson.put("resourceName", "车身");
        dataJson.put("startDate", "中信科技有限公司");
        dataJson.put("endDate", "李新华");
        dataJson.put("content", "15928465978");
        dataJson.put("publishTime", "2016-09-23");
        dataJson.put("publishOffTime", "2016-10-23");
        array.add(dataJson);
        dataJson.put("billId", "1");
        dataJson.put("resourceName", "车身");
        dataJson.put("startDate", "中信科技有限公司");
        dataJson.put("endDate", "李新华");
        dataJson.put("content", "15928465978");
        dataJson.put("publishTime", "2016-09-23");
        dataJson.put("publishOffTime", "2016-10-23");
        array.add(dataJson);
        dataJson.put("billId", "1");
        dataJson.put("resourceName", "车身");
        dataJson.put("startDate", "中信科技有限公司");
        dataJson.put("endDate", "李新华");
        dataJson.put("content", "15928465978");
        dataJson.put("publishTime", "2016-09-23");
        dataJson.put("publishOffTime", "2016-10-23");
        array.add(dataJson);

        returnJson.put("total", array.size());
        returnJson.put("rows", array);
        return returnJson;
    }
}